/**
    GpsNetwork lets you download files and log some system parameters from NetRs DGPS receivers.
    
    Copyright (C) 2009  Eduardo Maureira
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    email:emaureir@gmail.com
 */
package gui;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import netrs.EventoMessage;
import netrs.IEventoMessageListener;
import netrs.Receptor;

import org.apache.log4j.Logger;


/* Used by InternalFrameDemo.java. */
public class NetRsIFrame extends JInternalFrame implements IEventoMessageListener {
	private Receptor receptor;
	private JTextPane consola;
	private StyledDocument doc ;
	/**
	 * 
	 */
	private static final long serialVersionUID = 79982125771229894L;
	static int openFrameCount = 0;
	static final int xOffset = 30, yOffset = 30;
	private static final Logger logger = Logger.getLogger(NetRsIFrame.class);	

	public NetRsIFrame(Receptor _receptor) {
		super(_receptor.getSystemName(), 
				true, //resizable
				true, //closable
				true, //maximizable
				true);//iconifiable
		logger.info("Nuevo InternalFrame");
		this.receptor = _receptor;

		//...Create the GUI and put it in the window...
		JScrollPane jscroll;
		jscroll = new JScrollPane();

		consola = new JTextPane();
		jscroll.setViewportView(consola);
		consola.setAutoscrolls(true);
		this.getContentPane().add(jscroll);

		doc = this.consola.getStyledDocument();
		try {
			StringBuilder mensaje;
			mensaje = new StringBuilder ();
			mensaje.append("Consola del ");
			mensaje.append(receptor.getSystemName());
			mensaje.append("\r\n");
			doc.insertString(doc.getLength(), mensaje.toString(),doc.getStyle("regular"));
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.receptor.addMyEventListener(this);

		//...Then set the window size or call pack...
		setSize(300,300);

		//Set the window's location.
		setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
	}

	@Override
	public void handleMessageEvent(EventoMessage me) {
		try {
			doc.insertString(doc.getLength(), me.getMensaje().concat("\r\n"),doc.getStyle("regular"));
			consola.setCaretPosition(doc.getLength());
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
